package com.android.job.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.job.fusion.FusionCode;
import com.android.job.logic.SearchResumeLogic;
import com.android.job.model.Resume;
import com.android.job.model.ResumeItem;
import com.android.job.ui.adapter.ResumeItemAdapter;

/**
 * 搜索简历界面
 * 
 * 
 */
public class ResumeActivity extends BaseActivity implements
		android.view.View.OnClickListener, OnClickListener, OnItemClickListener
{

	private TextView salary_range;
	private TextView region;
	private TextView job_type;
	private TextView degree;
	private TextView update_time;
	private TextView num;
	private TextView loadMore;

	private ImageButton popup_chose_salary;
	private ImageButton popup_chose_region;
	private ImageButton popup_chose_job_type;
	private ImageButton popup_chose_degree;
	private ImageButton popup_chose_update_time;

	private Button search;

	private ListView resume_list;

	private ImageView search_back;

	private RelativeLayout resume_search;
	private RelativeLayout resume_search_result;

	private RelativeLayout salary_layout;
	private RelativeLayout region_layout;
	private RelativeLayout type_layout;
	private RelativeLayout degree_layout;
	private RelativeLayout updatetime_layout;

	private static int whichDialog;
	private ProgressDialog pd;
	private ArrayList<Resume> resumeList;

	private String salary_range_key = "";
	private String region_key = "";
	private String job_type_key = "";
	private String degree_key = "";
	private String update_time_key = "";
	private View loadMoreView;
	private int more = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resume_search);
		resumeList = new ArrayList<Resume>();
		initLayout();
		initListener();
	}

	private void initLayout()
	{
		pd = new ProgressDialog(ResumeActivity.this);
		pd.setCancelable(true);
		pd.setMessage(getString(R.string.search_loading));
		salary_range = (TextView) findViewById(R.id.salary_range_et_id);
		region = (TextView) findViewById(R.id.region_et_id);
		job_type = (TextView) findViewById(R.id.job_type_et_id);
		degree = (TextView) findViewById(R.id.degree_et_id);
		update_time = (TextView) findViewById(R.id.update_time_et_id);
		num = (TextView) findViewById(R.id.num);
		popup_chose_salary = (ImageButton) findViewById(R.id.popup_chose_salary);
		popup_chose_region = (ImageButton) findViewById(R.id.popup_chose_region);
		popup_chose_job_type = (ImageButton) findViewById(R.id.popup_chose_job_type);
		popup_chose_degree = (ImageButton) findViewById(R.id.popup_chose_degree);
		popup_chose_update_time = (ImageButton) findViewById(R.id.popup_chose_update_time);

		search = (Button) findViewById(R.id.search_bt_id);

		resume_list = (ListView) findViewById(R.id.result_list_lv_id);

		resume_search_result = (RelativeLayout) findViewById(R.id.resume_search_result);
		resume_search = (RelativeLayout) findViewById(R.id.resume_search);

		search_back = (ImageView) findViewById(R.id.search_back);

		salary_range.setInputType(InputType.TYPE_NULL);
		region.setInputType(InputType.TYPE_NULL);
		job_type.setInputType(InputType.TYPE_NULL);
		degree.setInputType(InputType.TYPE_NULL);
		update_time.setInputType(InputType.TYPE_NULL);

		salary_layout = (RelativeLayout) findViewById(R.id.salary);
		region_layout = (RelativeLayout) findViewById(R.id.region);
		type_layout = (RelativeLayout) findViewById(R.id.job_type);
		degree_layout = (RelativeLayout) findViewById(R.id.degree);
		updatetime_layout = (RelativeLayout) findViewById(R.id.update_time);

		loadMoreView = getLayoutInflater().inflate(R.layout.load_more, null);
		loadMore = (TextView) loadMoreView.findViewById(R.id.add_more);
		loadMore.setOnClickListener(ResumeActivity.this);
		resume_list.addFooterView(loadMoreView);

		salary_layout.setOnClickListener(this);
		region_layout.setOnClickListener(this);
		type_layout.setOnClickListener(this);
		degree_layout.setOnClickListener(this);
		updatetime_layout.setOnClickListener(this);

	}

	private void initListener()
	{
		popup_chose_salary.setOnClickListener(this);
		popup_chose_region.setOnClickListener(this);
		popup_chose_job_type.setOnClickListener(this);
		popup_chose_degree.setOnClickListener(this);
		popup_chose_update_time.setOnClickListener(this);
		search.setOnClickListener(this);
		search_back.setOnClickListener(this);
		resume_list.setOnItemClickListener(this);
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		Dialog dialog = null;
		AlertDialog.Builder builder = null;
		switch (id)
		{
		case FusionCode.RESUME_SEARCH_SALARY_RANGE:
			builder = new AlertDialog.Builder(this).setItems(
					R.array.salary_range, this);
			dialog = builder.create();
			whichDialog = FusionCode.RESUME_SEARCH_SALARY_RANGE;
			break;
		case FusionCode.RESUME_SEARCH_REGION:
			builder = new AlertDialog.Builder(this).setItems(R.array.region,
					this);
			dialog = builder.create();
			whichDialog = FusionCode.RESUME_SEARCH_REGION;
			break;
		case FusionCode.RESUME_SEARCH_JOB_TYPE:
			builder = new AlertDialog.Builder(this).setItems(R.array.job_type,
					this);
			dialog = builder.create();
			whichDialog = FusionCode.RESUME_SEARCH_JOB_TYPE;
			break;
		case FusionCode.RESUME_SEARCH_DEGREE:
			builder = new AlertDialog.Builder(this).setItems(R.array.degree,
					this);
			dialog = builder.create();
			whichDialog = FusionCode.RESUME_SEARCH_DEGREE;
			break;
		default:
			return super.onCreateDialog(id);
		}
		return dialog;
	}

	@Override
	protected void onResume()
	{
		super.onResume();
	}

	private Handler handler = new Handler()
	{

		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
			case FusionCode.NETWORK_ERROR:
				pd.dismiss();
				Toast.makeText(ResumeActivity.this, R.string.network_error,
						Toast.LENGTH_SHORT).show();

				break;
			case FusionCode.NETWORK_TIMEOUT_ERROR:
				pd.dismiss();
				Toast.makeText(ResumeActivity.this, R.string.network_timeout,
						Toast.LENGTH_SHORT).show();

				break;
			case FusionCode.RETURN_JSONOBJECT:
				pd.dismiss();
				Log.e("free.lang", "===RETURN_JSONOBJECT=====");
				ArrayList<Resume> tempList = (ArrayList<Resume>) msg.obj;
				resumeList.addAll(tempList);
				List<ResumeItem> itemList = new ArrayList<ResumeItem>();
				for (int i = 0; i < resumeList.size(); i++)
				{
					ResumeItem item = new ResumeItem();
					item.degree = resumeList.get(i).getDegree();
					item.realName = resumeList.get(i).getRealName();
					item.gender = resumeList.get(i).getGender();
					item.salary = resumeList.get(i).getSalary();
					itemList.add(item);
				}
				ResumeItemAdapter resumeItemAdapter = new ResumeItemAdapter(
						ResumeActivity.this, itemList);
				resume_list.setAdapter(resumeItemAdapter);
				num.setText(getString(R.string.result_start)
						+ resumeList.size() + getString(R.string.result_end));
				resume_search.setVisibility(View.GONE);
				resume_search_result.setVisibility(View.VISIBLE);
				break;
			case FusionCode.SERVER_ERROR:
				pd.dismiss();
				String errorInif = (String) msg.obj;

				Toast.makeText(ResumeActivity.this, errorInif,
						Toast.LENGTH_SHORT).show();
				break;
			}

		};
	};

	// private void fillList()
	// {
	//
	// resume_search.setVisibility(View.GONE);
	// resume_search_result.setVisibility(View.VISIBLE);
	// ArrayList<ResumeItem> resumeList = new ArrayList<ResumeItem>();
	// ResumeItem one = new ResumeItem();
	// ResumeItem two = new ResumeItem();
	// one.realName = "黎明";
	// one.degree = "高中";
	// one.gender = "男";
	// two.realName = "小泽";
	// two.degree = "小学";
	// two.gender = "女";
	// resumeList.add(one);
	// resumeList.add(two);
	// ResumeItemAdapter resumeItemAdapter = new ResumeItemAdapter(this,
	// resumeList);
	// resume_list.setAdapter(resumeItemAdapter);
	// }

	@Override
	public void onClick(View view)
	{
		Intent intent = new Intent();
		switch (view.getId())
		{
		case R.id.popup_chose_salary:
			// showDialog(FusionCode.RESUME_SEARCH_SALARY_RANGE);
			// intent.setClass(this, SelectActivity.class);
			// intent.putExtra("type", FusionCode.SALARY_ITEM);
			// startActivityForResult(intent, 0);
			// jumpToSelect(FusionCode.SALARY_ITEM, R.id.salary_range_et_id);
			break;
		case R.id.popup_chose_region:
			// jumpToSelect(FusionCode.CITY_ITEM_ONE, R.id.region_et_id);
			break;
		case R.id.popup_chose_job_type:
			// intent.setClass(this, SelectActivity.class);
			// intent.putExtra("type", FusionCode.JOB_TYPE_ITEM);
			// startActivityForResult(intent, 0);
			// jumpToSelect(FusionCode.JOB_TYPE_ITEM, R.id.job_type_et_id);
			break;
		case R.id.popup_chose_degree:
			jumpToSelect(FusionCode.DEGREE_ITEM, R.id.degree_et_id);
			break;
		case R.id.popup_chose_update_time:
			jumpToSelect(FusionCode.TIME_ITEM, R.id.update_time_et_id);
			break;
		case R.id.search_bt_id:
			pd.show();
			resumeList.clear();
			SearchResumeLogic.getInstance().searchResume(handler,
					salary_range_key, region_key, job_type_key, degree_key,
					update_time_key, "0");
			break;
		case R.id.search_back:
			onBack();
			break;
		case R.id.salary:
			jumpToSelect(FusionCode.SALARY_ITEM, R.id.salary_range_et_id);
			break;
		case R.id.region:
			jumpToSelect(FusionCode.CITY_ITEM_ONE, R.id.region_et_id);
			break;
		case R.id.job_type:
			jumpToSelect(FusionCode.JOB_TYPE_ITEM, R.id.job_type_et_id);
			break;
		case R.id.degree:
			jumpToSelect(FusionCode.DEGREE_ITEM, R.id.degree_et_id);
			break;
		case R.id.update_time:
			jumpToSelect(FusionCode.TIME_ITEM, R.id.update_time_et_id);
			break;
		case R.id.add_more:
			pd.show();
			more = more + 20;
			SearchResumeLogic.getInstance().searchResume(handler,
					salary_range_key, region_key, job_type_key, degree_key,
					update_time_key, String.valueOf(more));
			break;

		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (requestCode == 0 && resultCode == 0)
		{
			if (data != null)
			{
				int id = data.getIntExtra("view_id", 0);
				String choose = data.getStringExtra("chose");
				String text = data.getStringExtra("text");
				switch (id)
				{
				case R.id.salary_range_et_id:
					salary_range.setText(text);
					salary_range_key = choose;
					break;
				case R.id.region_et_id:
					region.setText(text);
					region_key = choose;
					break;
				case R.id.job_type_et_id:
					job_type.setText(text);
					job_type_key = choose;
					break;
				case R.id.degree_et_id:
					degree.setText(text);
					degree_key = choose;
					break;
				case R.id.update_time_et_id:
					update_time.setText(text);
					update_time_key = choose;
					break;
				default:
					break;
				}
			}

		}

		super.onActivityResult(requestCode, resultCode, data);

	}

	@Override
	public void onClick(DialogInterface dialog, int which)
	{
		switch (whichDialog)
		{
		case FusionCode.RESUME_SEARCH_SALARY_RANGE:
			salary_range.setText(ResumeActivity.this.getResources()
					.getStringArray(R.array.salary_range)[which]);
			break;
		case FusionCode.RESUME_SEARCH_REGION:
			region.setText(ResumeActivity.this.getResources().getStringArray(
					R.array.region)[which]);
			break;
		case FusionCode.RESUME_SEARCH_JOB_TYPE:
			job_type.setText(ResumeActivity.this.getResources().getStringArray(
					R.array.job_type)[which]);
			break;
		case FusionCode.RESUME_SEARCH_DEGREE:
			degree.setText(ResumeActivity.this.getResources().getStringArray(
					R.array.degree)[which]);
			break;
		}
	}

	@Override
	public void onBackPressed()
	{
		onBack();
	}

	public void onBack()
	{
		if (resume_search.getVisibility() == View.GONE
				&& resume_search_result.getVisibility() == View.VISIBLE)
		{
			resume_search.setVisibility(View.VISIBLE);
			resume_search_result.setVisibility(View.GONE);
			resumeList.clear();
			// clearKey();
		} else if (resume_search.getVisibility() == View.VISIBLE
				&& resume_search_result.getVisibility() == View.GONE)
		{
			this.finish();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3)
	{
		int size = resumeList.size();
		if (position < size)
		{
			if (isLogin() && (getLoginType().equals("1")))
			{
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putSerializable("resumedetail", resumeList.get(position));
				intent.putExtras(bundle);
				intent.setClass(this, ResumeDetailActivity.class);
				startActivity(intent);

			}
			else if (isLogin() && (getLoginType().equals("0")))
			{
				Toast.makeText(ResumeActivity.this, "个人不可以查看简历",
						Toast.LENGTH_SHORT).show();
			}
			else if (!isLogin())
			{
				Toast.makeText(ResumeActivity.this, "请到主页登录",
						Toast.LENGTH_SHORT).show();
			}
		}
		else
		{
			pd.show();
			more = more + 20;
			SearchResumeLogic.getInstance().searchResume(handler,
					salary_range_key, region_key, job_type_key, degree_key,
					update_time_key, String.valueOf(more));
		}
	}

	private void clearKey()
	{
		salary_range_key = "";
		region_key = "";
		job_type_key = "";
		degree_key = "";
		update_time_key = "";
		salary_range.setText("");
		region.setText("");
		job_type.setText("");
		degree.setText("");
		update_time.setText("");
	}

}
